package cn.cds.withyou.admin.stores;

import android.support.annotation.NonNull;

import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import cn.cds.withyou.admin.Application;
import cn.cds.withyou.admin.actions.FeedbackActions;
import cn.cds.withyou.lib.model.FeedbackDto;
import cn.ljs.android.buses.ActionBus;
import cn.ljs.android.buses.DataBus;
import cn.ljs.android.store.Store;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 7/5/2016 23:13
 */
public class FeedbackStore extends Store<FeedbackActions> {

    private static FeedbackStore INSTANCE;

    public static FeedbackStore get () {
        if (null == INSTANCE) {
            INSTANCE = new FeedbackStore(Application.getInstance().getActionsBus(), Application.getInstance().getDataBus());
        }
        return INSTANCE;
    }

    private List<FeedbackDto> mData = new ArrayList<>();

    public List<FeedbackDto> getData () {
        return mData;
    }

    protected FeedbackStore(@NonNull ActionBus actionBus, @NonNull DataBus dataBus) {
        super(actionBus, dataBus);
    }

    @Override
    public void onAction(FeedbackActions actions) {
        FeedbackEvent event = new FeedbackEvent();
        switch (actions.getAction()) {
            case FeedbackActions.LOAD_FEEDBACK_SUCC:
                event.code = FeedbackEvent.LOAD_FEEDBACK_SUCC;
                mData.clear();
                mData.addAll((Collection<? extends FeedbackDto>) actions.getData().get(FeedbackActions.KEY_DATA_INFO.hashCode(), new ArrayList<>()));
                break;
            case FeedbackActions.LOAD_FEEDBACK_FAIL:
                event.code = FeedbackEvent.LOAD_FEEDBACK_FAIL;
                break;
            default:
                return;
        }
        mDataBus.dispatch(event);
    }

    @Subscribe
    @Override
    public void onEventMainThread(FeedbackActions actions) {
        super.onEventMainThread(actions);
    }

    public static class FeedbackEvent extends StoreChangeEvent {
        public final static int LOAD_FEEDBACK_SUCC = 0x01;
        public final static int LOAD_FEEDBACK_FAIL = 0x02;
    }
}
